﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.UserControl;


namespace Hisun.View.Management
{
    public partial class UserRightSetupSelectRight : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                TextBox_UserId.Text = Request.QueryString["userid"].ToString();
                var myUserQuery = (from a in db.TSCRB_JS_Users where a.操作员ID == TextBox_UserId.Text select a).Single();
                TextBox_UserName.Text = myUserQuery.操作员姓名;
                if (myUserQuery.权限ID == "" || myUserQuery.权限ID == null)
                {
                    var QueryToGridView = from b in db.TSCRB_JS_Auths
                                          select new
                                          {
                                              b.权限ID,
                                              b.权限名称,
                                              check = false
                                          };
                    GridView_RoleList.DataSource = QueryToGridView;
                    GridView_RoleList.DataBind();
                }
                else
                { 
                string[] aa = myUserQuery.权限ID.Split(',');

                var QueryToGridView = from b in db.TSCRB_JS_Auths
                                      select new
                                      {
                                          b.权限ID,
                                          b.权限名称,
                                          check = aa.Contains(b.权限ID.ToString())
                                      };
                GridView_RoleList.DataSource = QueryToGridView;
                GridView_RoleList.DataBind();

                }

                


             

            }

        }

        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {
            string myRight = "";

            for (int i = 0; i < GridView_RoleList.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_RoleList.Rows[i];
                if (((CheckBox)myrows.Cells[2].FindControl("Checkbox_Right")).Checked)
                {
                    myRight = myRight + "," + myrows.Cells[0].Text;
                }


            }

            //var IsExist = from a in db.TAbc_JS_Users where a.操作员ID == TextBox_UserId.Text select a;
            //if (IsExist.Count() == 0)
            //{ 
            
            
            //}


            var myRightRow = (from a in db.TSCRB_JS_Users where a.操作员ID == TextBox_UserId.Text select a).Single();

            if (myRight != "")
            {
                string xx = myRight.Substring(1, myRight.Length - 1);
                myRightRow.权限ID = xx;
            }
            else
            {
                myRightRow.权限ID = "";
            }

            myRightRow.拥有权限ID = GetUserRight(TextBox_UserId.Text);


            try
            {
                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新权限成功');history.go(-2); </script>");
                //history.go(-2)用于返回到角色管理的主页面。
            }
            catch
            {

                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新权限失败'); </script>");
            }
        }


        private string GetUserRight(string UserId)
        {

            var myRightRow = (from a in db.TSCRB_JS_Users where a.操作员ID == UserId select a).Single();
            string myright1 = myRightRow.权限ID;
            var myroleright = (from c in db.TSCRB_JS_Role where myRightRow.角色ID.Split(',').Contains(c.角色ID.ToString()) select c.权限ID).ToArray();
            string[] finalright = myright1.Split(',');

            foreach (string myrigh2 in myroleright)
            {

                finalright = Join(myrigh2.Split(','), finalright);

            }
            return String.Join(",", finalright);

        }

        private string[] Join(string[] arr_str1, string[] arr_str2)
        {

            var q = (from c in arr_str1 select c).Union(from d in arr_str2 select d);
            var q1 = from x in q.ToArray() where x != string.Empty select x;
            return q1.ToArray();
        }

        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            Server.Transfer("~/View/Management/UserRightSetup.aspx");
        }
    }
}
